<?php

class Candidate extends Controller {

	function Candidate()
	{
		parent::Controller();
		if(! $this->dx_auth->is_logged_in()	){
			redirect('/auth/login/', 'refresh');
		}
		
		$this->load->model('Candidates','cand');
		$this->load->model('Positions','pos');
		$this->load->library('form_validation');
	}
	
	function show($name , $id)
	{
		
		
		$qry = $this->cand->get_cand_by_id($id);
		
			$var['candidate'] = $qry->row_array();
		
		$qry2 = $this->pos->get_pos_by_id($var['candidate']['position_id']);
		
			$var['position'] = $qry2->row_array();
			
		
		$var['title_for_layout'] = "Candidate Information";
		$this->layout->view('candidates/show',$var);
	}
	
	function newCandidate($name, $id){
		
			$var['default_pos'] = $id;
			$var['title_for_layout'] = "New Position";
			$var['positionList'] = $this->_getPositionByEvent($id);
			$this->layout->view('candidates/new', $var);
	}
	
	function submit(){
		
		$this->form_validation->set_rules('cand_name','Cand_name','max_length[75]');			
		$this->form_validation->set_rules('position_id','Position_id','max_length[11]');			
		$this->form_validation->set_rules('cand_desc','Cand_desc','max_length[255]');
			
		$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
	
		if ($this->form_validation->run() == FALSE) // validation hasn'\t been passed
		{
			$var['title_for_layout'] = "New Position";
			$this->layout->view('myform_view', $var);
		}
		else // passed validation proceed to post success logic
		{
		 	// build array for the model
			
			$form_data = array(
					       	'cand_name' => set_value('cand_name'),
					       	'position_id' => set_value('position_id'),
					       	'cand_desc' => set_value('cand_desc')
						);
					
			// run insert model to write data to db
		
			if ($this->cand->saveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect('position/show/id/'.$form_data['position_id']);   // or whatever logic needs to occur
			}
			else
			{
			echo 'An error occurred saving your information. Please try again later';
			// Or whatever error handling is necessary
			}
		}
		
	}
	
	function _getPositionByEvent($id){
		$qry = $this->pos->get_pos_by_id($id);
		$posinfo = $qry->row_array();
		
		$qry2 = $this->pos->get_all_of_event($posinfo['event_id']);
		$arr = array();
		foreach($qry2->result() as $row){
			$arr[$row->id] = $row->pos_name;
		}
		return $arr;
}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */